package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
        return vehicleVOS;
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        //vehicleSaveParam的数据赋值给Vehicle对象
        Vehicle vehicle = new Vehicle();
        //利用BeanUtils进行相同属性的赋值
        //第一个参数，是原有的数据，第二个参数是数据的目的地
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        //判断是新增还是修改
        if(vehicleSaveParam.getId()==null){
            //id为空，进行新增操作
            //新增车辆的状态一定是空闲(1)
            vehicle.setStatus("1");
            //赋值创建时间，系统时间
            vehicle.setCreateTime(new Date());
            vehicleMapper.insert(vehicle);
        }else {
            //根据id去修改数据
            //给修改时间的属性进行赋值
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        vehicleMapper.deleteVehicle(id);
    }

    @Override
    public void unBindVehicle(Long vehicleId) {
        //初始化一个Vehicle对象
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        //围栏id  null
        vehicle.setGeofenceId(null);
        //围栏绑定状态 0
        vehicle.setGeofenceBindStatus("0");
        //更新时间
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public List<VehicleVO> selectVehicleByGeoId(Long geoId) {
        List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicleByGeoId(geoId);
        return vehicleVOS;
    }

    @Override
    public void bindVehicle(Long geofenceId, Long vehicleId) {
        //初始化车辆对象
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceId(geofenceId);
        //围栏状态，绑定状态是1
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
